import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:lib_common/components/widget/ntt_back_button.dart';
import 'package:lib_common/components/widget/ntt_ordinary_button.dart';
import 'package:lib_common/components/widget/ntt_switch.dart';
import 'package:lib_common/components/widget/ntt_stroke_text.dart';
import 'package:lib_common/lib_common.dart';
import 'package:lib_common/utils/ntt_dialog_utils.dart';
import 'package:lib_common/utils/ntt_toast_utils.dart';
import 'package:lib_common_example/error_page.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  final _libCommonPlugin = LibCommon();

  bool _isSwitch = false;

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      platformVersion = await _libCommonPlugin.getPlatformVersion() ??
          'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    final botToastBuilder = BotToastInit();
    // ScreenUtil.init(context, designSize: Size(720, 1280));
    return MaterialApp(
      builder: (context, child) {
        child = botToastBuilder(context, child);
        return child;
      },
      navigatorObservers: [BotToastNavigatorObserver()],
      home: Scaffold(
          appBar: AppBar(
            title: const Text('Plugin example app'),
          ),
          body: Builder(builder: (context) {
            NttDialogUtils.instance.init(context, 720, 1280);
            return SizedBox(
              width: MediaQuery.of(context).size.width,
              child: SingleChildScrollView(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisSize: MainAxisSize.max,
                  children: [
                    NttSwitch(
                      _isSwitch,
                      (p0) {
                        setState(() {
                          _isSwitch = p0;
                        });
                      },
                      width: 150,
                      height: 50,
                    ),
                    NttBackButton(
                      width: 44,
                      height: 44,
                      onTap: () {
                        debugPrint("点击了返回按钮");
                      },
                    ),
                    Text(
                      'Running on: $_platformVersion\n',
                      textAlign: TextAlign.center,
                    ),
                    ElevatedButton(
                        onPressed: () {
                          NttDialogUtils.instance.init(context, 720, 1280);
                          // ScreenUtil.init(context, designSize: Size(720, 1280));
                        },
                        child: const Text("初始化")),
                    ElevatedButton(
                        onPressed: () {
                          debugPrint(
                              "当前屏幕状态-> ${MediaQuery.of(context).orientation}");
                          if (MediaQuery.of(context).orientation ==
                              Orientation.landscape) {
                            SystemChrome.setPreferredOrientations([
                              DeviceOrientation.portraitUp,
                              DeviceOrientation.portraitDown
                            ]);
                          } else {
                            SystemChrome.setPreferredOrientations([
                              DeviceOrientation.landscapeLeft,
                              DeviceOrientation.landscapeRight
                            ]);
                          }
                        },
                        child: const Text("切换横竖屏")),
                    ElevatedButton(
                        onPressed: () {
                          setState(() {});
                          NttDialogUtils.instance.showStandardDialog(
                            title: "这是标题",
                            content: "这是内容",
                            cancelText: "取消",
                            confirmText: "确认",
                            onCancel: () {
                              debugPrint('取消事件');
                            },
                            onConfirm: () {
                              debugPrint('确认事件');
                            },
                            barrierDismissible: true,
                            context: context,
                          );
                        },
                        child: const Text("显示标准确认弹窗")),
                    ElevatedButton(
                        onPressed: () {
                          setState(() {});
                          NttDialogUtils.instance.showInputDialog(
                            title: "这是标题",
                            cancelText: "取消",
                            confirmText: "确认",
                            onCancel: () {
                              debugPrint('取消事件');
                            },
                            onConfirm: (text) {
                              debugPrint('确认事件,$text');
                            },
                            barrierDismissible: true,
                            context: context,
                          );
                        },
                        child: const Text("显示带输入框的弹窗")),
                    ElevatedButton(
                        onPressed: () {
                          setState(() {});
                          NttDialogUtils.instance.showSelectedDialog(
                            title: "这是标题",
                            barrierDismissible: true,
                            datas: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
                            defaultSelects: ['周一', '周三', '周五'],
                            isSingleSelect: true,
                            onConfirm: (value) {
                              debugPrint('确认事件,$value');
                            },
                            context: context,
                          );
                        },
                        child: const Text("显示多选弹窗")),
                    ElevatedButton(
                      onPressed: () {
                        NttToastUtils.showToast('text');
                      },
                      child: const Text("显示toast"),
                    ),
                    ElevatedButton(
                      onPressed: () {
                        NttToastUtils.showErrorToast(
                            "显示带错误状态的toast显示带错误状态的toast显示带错误状态的toast显示带错误状态的toast");
                      },
                      child: const Text("显示带错误状态的toast"),
                    ),
                    ElevatedButton(
                      onPressed: () {
                        NttToastUtils.showDefaultLoading();
                      },
                      child: const Text("显示默认loading"),
                    ),
                    ElevatedButton(
                      onPressed: () {
                        NttToastUtils.showLoading();
                      },
                      child: const Text("显示自定义loading"),
                    ),
                    ElevatedButton(
                      onPressed: () {
                        Navigator.of(context).push(MaterialPageRoute(builder: (
                          BuildContext context,
                        ) {
                          return ErrorPage();
                        }));
                      },
                      child: const Text("显示错误视图"),
                    ),
                    const NttOrdinaryButton(
                      "text",
                      width: 50,
                      height: 40,
                    ),
                    NttStrokeText("Hello，这是描边文本"),
                  ],
                ),
              ),
            );
          })),
    );
  }
}
